﻿using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Serialization;

namespace HeThongThongTin.Library
{
    [Serializable()]
    public class DM_CapNganhList : ReadOnlyListBase<DM_CapNganhList, DM_CapNganhInfo>
    {
        #region Business methods
        public System.Collections.Generic.IList<DM_CapNganhInfo> getListOfInfo()
        {
            return this.Items;
        }
        #endregion

        #region Factory Methods
        public static void GetDM_CapNganhList(string criteria, EventHandler<DataPortalResult<DM_CapNganhList>> callback)
        {
            var dp = new DataPortal<DM_CapNganhList>();
            dp.FetchCompleted += callback;
            dp.BeginFetch(new SingleCriteria<DM_CapNganhList, string>(criteria));
        }

#if !SILVERLIGHT
        public static DM_CapNganhList GetDM_CapNganhList()
        {
            return DataPortal.Fetch<DM_CapNganhList>();
        }

        private DM_CapNganhList()
        { /* require use of factory methods */ }
#else
		public DM_CapNganhList()
		{ /* require by MobileFormatter */ }
#endif
        #endregion

        #region Root object data access
#if !SILVERLIGHT
        private void DataPortal_Fetch(SingleCriteria<DM_CapNganhList, string> criteria)
        {
            try
            {
                using (var ctx =
                   ContextManager<HeThongThongTin.DataAccess.HeThongThongTinDataContext>.
                   GetManager(HeThongThongTin.DataAccess.Database.HeThongThongTin))
                {
                    //get DM_CapNganh data
                    if (criteria.Value == BusinessConfiguration.Configuration.GridViewFetchMode)
                    {
                        var data = (from p in ctx.DataContext.DM_CapNganhs
                                    select new DM_CapNganhInfo(p.Khoa, p.Ma, p.Ten, p.GhiChu));
                        IsReadOnly = false;
                        this.AddRange(data);
                        IsReadOnly = true;
                    }

                    if (criteria.Value == BusinessConfiguration.Configuration.ComboboxFetchMode)
                    {
                        var data = (from p in ctx.DataContext.DM_CapNganhs
                                    select new DM_CapNganhInfo(p.Khoa, p.Ma, p.Ten));
                        IsReadOnly = false;
                        this.AddRange(data);
                        IsReadOnly = true;
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


#endif
        #endregion
    }
}

